package com.bootdo.common.utils.text;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 * 
 * @author longing
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{


    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     * 
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * @author XZW
     * @date 2020-06-08
     * @deacribe 获取当前年份
     * @log
     */
    public static int getYear() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        return year;
    }

    /**
     * @author XZW
     * @date 2020-06-08
     * @deacribe 获取当前月份
     * @log
     */
    public static int getMonth() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        return month;
    }
    public static String getYearMonth() {
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM");
        return formatters.format(now);
    }

    /**
     * @author XZW
     * @date 2020-06-08
     * @deacribe 获取当前日期
     * @log
     */
    public static int getDay() {
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        return day;
    }

    /**
     * @author XZW
     * @date 2020-06-08
     * @deacribe 获取当前月份,15号之前返回上一月日期,之后返回当前月(包括15号)
     * @log
     */
    public static String getCarMonth() {
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        cal.setTime(new Date());
        if (day < 15) {
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
        } else {
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(cal.getTime());
    }

    /**
     * @author XZW
     * @date 2020-06-09
     * @deacribe 获取指定几个月之前的时间
     * @log
     */
    public static Date getTimeBeforeNum(int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-num);
        // 获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //将小时至0
        cal.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        cal.set(Calendar.MINUTE, 0);
        //将秒至0
        cal.set(Calendar.SECOND,0);
        //将毫秒至0
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * @author XZW
     * @date 2020-06-09
     * @deacribe 将时间字符按照指定格式转为Date格式
     * @log
     */
    public static Date strParseDate(String format, String time) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author XZW
     * @date 2020-06-09
     * @deacribe 判断当前日期是否为1-20号
     * @log
     */
    public static boolean is20Before() {
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        if (day <= 20) {
            return true;
        }
        return false;
//        return  true;
    }

    /**
     * @author XZW
     * @date 2020-07-09
     * @deacribe 获取上月时间
     * @log
     */
    public static String getLastMonth() {
//        Calendar cal = Calendar.getInstance();
//        int day = cal.get(Calendar.DATE);
//        cal.setTime(new Date());
//        System.out.println(cal.getTime());
//        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
//        System.out.println(cal.getTime());
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
//        return format.format(cal.getTime());
        LocalDate today = LocalDate.now();
        today = today.minusMonths(1);
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM");
        return formatters.format(today);
    }
    /**
     * @author WCG
     * @date 2022-09-28
     * @deacribe 获取两个时间中所有月份
     * @log
     */
    public static  List<String> getEveryMonth(Date startTime,Date endTime) {

        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(startTime);
        String format2 = format.format(endTime);


        LocalDate dateBefore= LocalDate.parse(format1);
        LocalDate dateAfter = LocalDate.parse(format2);

        int years = dateAfter.getYear() - dateBefore.getYear();

        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM");

        int months = years * 12 + (dateAfter.getMonthValue() - dateBefore.getMonthValue());
        List<String> monthList = new ArrayList<>();
        for (int i = 0; i <= months; i++) {
            LocalDate localDate = dateBefore.plusMonths(i);
            String format3 = formatters.format(localDate);
            monthList.add(format3);
        }

        return monthList;
    }





    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM");
        System.out.println(formatters.format(now));


    }
}
